package com.bbm.communitysystem.core.redis;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class Redis {

    @Value("${redis.key}")
    private String key;

    @Getter
    private final RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public Redis(@Qualifier("main") RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    public void setValue(String keys, Object value) {
        redisTemplate.opsForValue().set(key + keys, value + "");
    }

    /**
     * 设置过期时间
     * @param value 值
     * @param expire 过期时间 分钟
     */
    public void setValue(String keys, Object value, long expire) {
        redisTemplate.opsForValue().set(key + keys, value, expire, TimeUnit.MINUTES);
    }

    public Object getValue(String keys) {
        return redisTemplate.opsForValue().get(key + keys);
    }


    public void deleteValue(String keys) {
        redisTemplate.delete(key + keys);
    }
}
